package com.redsoil.house.ui.property.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ScrollView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.redsoil.house.R;
import com.redsoil.house.base.RootFragment;
import com.redsoil.house.model.bean.OnePropertyVO;
import com.redsoil.house.model.bean.RoomFileVO;
import com.redsoil.house.model.bean.TestVO;
import com.redsoil.house.ui.matter.activity.MatterFileListActivity;
import com.redsoil.house.ui.property.adapter.PropertyLeaseAdapter;
import com.redsoil.house.ui.property.contract.PropertyFileContract;
import com.redsoil.house.ui.property.contract.PropertyInfoContract;
import com.redsoil.house.ui.property.presenter.PropertyFilePresenter;
import com.redsoil.house.ui.property.presenter.PropertyInfoPresenter;
import com.redsoil.house.widget.PropertyFileItem;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 物业信息
 * 文件
 */
public class PropertyFileFragment extends RootFragment<PropertyFilePresenter> implements PropertyFileContract.View {

    @BindView(R.id.rv_list)
    RecyclerView rv_list;

    @BindView(R.id.scroll_view)
    ScrollView scroll_view;

    @BindView(R.id.property_build_list_layout)
    AutoLinearLayout property_build_list_layout;

    @BindView(R.id.business_related)
    PropertyFileItem business_related;

    @BindView(R.id.assets_property)
    PropertyFileItem assets_property;

    @BindView(R.id.tenancy_management)
    PropertyFileItem tenancy_management;

    @BindView(R.id.insurance_related)
    PropertyFileItem insurance_related;

    @BindView(R.id.tax_related)
    PropertyFileItem tax_related;

    @BindView(R.id.other)
    PropertyFileItem other;

    private PropertyLeaseAdapter propertyLeaseAdapter;

    private String propertyId;

    private String buildRoomName;

    private int showIndex = 0;

    public static PropertyFileFragment newInstance(String propertyId) {
        Bundle args = new Bundle();

        PropertyFileFragment fragment = new PropertyFileFragment();
        fragment.setArguments(args);
        args.putString("propertyId",propertyId);
        return fragment;
    }

    @Override
    protected void initEventAndData() {

        super.initEventAndData();

        propertyId = getArguments().getString("propertyId");

        stateLoading();

        mPresenter.getOneProperty(propertyId);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if(showIndex == 2 && propertyId != null){
            property_build_list_layout.setVisibility(View.VISIBLE);
            scroll_view.setVisibility(View.GONE);
        }
    }

    @Override
    public void getOnePropertySuccess(OnePropertyVO onePropertyVO) {
        //获取基础信息
        if(onePropertyVO.getRooms()!=null && onePropertyVO.getRooms().size() > 0){
            rv_list.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
            rv_list.setLayoutManager(new LinearLayoutManager(getContext()));

            propertyLeaseAdapter = new PropertyLeaseAdapter(R.layout.property_lease_list_item,onePropertyVO.getRooms());

            propertyLeaseAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    //只有 租赁中能进入查看
//                    state = 2
                    OnePropertyVO.RoomsBean data = (OnePropertyVO.RoomsBean) adapter.getData().get(position);

                    if(data.getState().equals("2")){
                        stateLoading();
                        buildRoomName = data.getRoomNum();
                        mPresenter.getRoomFile(data.getId());
                    }
                }
            });

            rv_list.setAdapter(propertyLeaseAdapter);

            stateMain();

        }else {
            buildRoomName = "全部";
            //请求接口
            mPresenter.getRoomFile(onePropertyVO.getRoomId());
        }
    }

    @Override
    public void getRoomFileSuccess(RoomFileVO roomFileVO) {

        for (int i= 0;i<roomFileVO.getFiles().size();i++){
            RoomFileVO.FilesBeanX filesBeanx = roomFileVO.getFiles().get(i);
            switch (filesBeanx.getTypeCode()){
                case "MMXG":
                    setPropertyFileItemData(business_related,filesBeanx,R.drawable.icon_business_related,getResources().getString(R.string.business_related),getResources().getDrawable(R.drawable.shape_top_edge));
                    break;
                case "ZWGX":
                    setPropertyFileItemData(assets_property,filesBeanx,R.drawable.icon_assets_property,getResources().getString(R.string.assets_property),getResources().getDrawable(R.drawable.shape_top_edge));
                    break;
                case "ZYGL":
                    setPropertyFileItemData(tenancy_management,filesBeanx,R.drawable.icon_tenancy_management,getResources().getString(R.string.tenancy_management),getResources().getDrawable(R.drawable.shape_top_edge));
                    break;
                case "BXXG":
                    setPropertyFileItemData(insurance_related,filesBeanx,R.drawable.icon_insurance_related,getResources().getString(R.string.insurance_related),getResources().getDrawable(R.drawable.shape_top_edge));
                    break;
                case "SWXG":
                    setPropertyFileItemData(tax_related,filesBeanx,R.drawable.icon_tax_related,getResources().getString(R.string.tax_related),getResources().getDrawable(R.drawable.shape_top_edge));
                    break;
                case "OTHER":
                    setPropertyFileItemData(other,filesBeanx,R.drawable.icon_other,getResources().getString(R.string.other),getResources().getDrawable(R.drawable.shape_top_bottom_edge));
                    break;
                default:
                    break;
            }
        }

        switch (buildRoomName){
            case "全部":
                break;
            case "一栋":
                tenancy_management.setVisibility(View.GONE);
                break;
            default:
                business_related.setVisibility(View.GONE);
                assets_property.setVisibility(View.GONE);
                insurance_related.setVisibility(View.GONE);
                tax_related.setVisibility(View.GONE);
                other.setVisibility(View.GONE);
                break;
        }

        showIndex = 2;

        property_build_list_layout.setVisibility(View.GONE);
        scroll_view.setVisibility(View.VISIBLE);

        stateMain();
    }

    private void setPropertyFileItemData(PropertyFileItem propertyFileItem, final RoomFileVO.FilesBeanX filesBeanx, Integer icon,String title,Drawable drawable){
        propertyFileItem.setData(icon,title,filesBeanx.getFileCount()+"",drawable);
        if(filesBeanx.getFileCount() > 0){
            propertyFileItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ArrayList<RoomFileVO.FilesBeanX.FilesBean> arrayList = new ArrayList<RoomFileVO.FilesBeanX.FilesBean>();
                    arrayList.addAll(filesBeanx.getFiles());

                    getContext().startActivity(MatterFileListActivity.newInstance(getContext(),arrayList));
                }
            });
        }
    }

    @Override
    public void responeError(String errorMsg) {
        stateError();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_property_file;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }
}
